package com.buer.storage.api;


import com.buer.storage.business.domain.Supplier;
import com.buer.storage.business.dto.SupplierDto;
import com.buer.storage.business.service.SupplierService;
import com.buer.storage.common.annotation.Auth;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.utils.BaseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;

    //增加供应商
    @Auth
    @PostMapping("/add.do")
    public BaseResult add(@RequestBody SupplierDto supplierDto) {
        if (supplierService.insert(supplierDto) > 0) {
            //添加供应商成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //添加供应商失败
        return BaseResult.build(ResultEnum.FAILURE);
    }
    //删除供应商
    @Auth
    @GetMapping("/delete.do")
    public BaseResult delete(int supplierId) {
        if (supplierService.delete(supplierId) > 0) {
            //删除供应商成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //删除供应商失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //修改供应商
    @Auth
    @PostMapping("/update.do")
    public BaseResult update(@RequestBody Supplier supplier){
        if (supplierService.update(supplier)>0){
            //修改供应商成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //修改供应商失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //查询所有供应商
    @Auth
    @GetMapping("/findAllSupplier.do")
    public BaseResult findAllSupplier(){
        List<Supplier> allSupplier = supplierService.findAllSupplier();
        return BaseResult.build(ResultEnum.SUCCESS,allSupplier);
    }



    @Auth
    @GetMapping("/searchSupplier.do")
    public BaseResult searchSupplier(String supplierCode,String supplierName){
        List<Supplier> list = supplierService.searchSupplier(supplierCode, supplierName);
        return BaseResult.build(ResultEnum.SUCCESS,list);
    }
}
